﻿using System;

namespace Homework17
{
    /// <summary>
    /// 打开上述文本文档，计算年龄和身高的平均值，再分别计算男女年龄和身高的平均值
    /// </summary>
    internal class Program
    {
        static void Main(string[] args)
        {
            {
                // filepath
                string filePath = @"D:\samples.txt";

                // 读取数据
                var data = File.ReadLines(filePath)
                          .Select(line => line.Split(','))
                          .Select(parts => new
                          {
                              Id = int.Parse(parts[0]),
                              Name = parts[1],
                              Age = int.Parse(parts[2]),
                              BirthDate = DateTime.Parse(parts[3]),
                              Height = double.Parse(parts[4]),
                              Gender = parts[5]
                          });

                // 计算所有人的平均年龄和身高
                double averageAge = data.Average(person => person.Age);
                double averageHeight = data.Average(person => person.Height);

                Console.WriteLine($"总体平均年龄: {averageAge:F2} 岁");
                Console.WriteLine($"总体平均身高: {averageHeight:F2} cm");

                // 分别计算男性和女性的平均年龄和身高
                var genderAverages = data.GroupBy(person => person.Gender)
                                         .Select(group => new
                                         {
                                             Gender = group.Key,
                                             AverageAge = group.Average(p => p.Age),
                                             AverageHeight = group.Average(p => p.Height)
                                         });

                foreach (var averages in genderAverages)
                {
                    Console.WriteLine($"{averages.Gender} 平均年龄: {averages.AverageAge:F2} 岁, 平均身高: {averages.AverageHeight:F2} cm");
                }
            }
        }
    }
}

    

